package com.linkhub.oms.common.util;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collection;

/**
 * @Description: QtObjectUtil
 * @Author: echo
 * @Date: 2022/11/29 21:10
 * @Version: 1.0
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class QtObjectUtil {

    /**
     * 判断对象是否属于文件类型
     */
    @SuppressWarnings("all")
    public static boolean isMultiPartFile(Object obj) {
        // 对象
        if (obj instanceof MultipartFile) {
            return true;
        }

        // 数组
        if (obj instanceof MultipartFile[]) {
            return true;
        }

        // 集合
        boolean instantOfFlag = obj instanceof Collection
                && CollectionUtils.isNotEmpty((Collection) obj)
                && ((Collection) obj).toArray()[0] instanceof MultipartFile;
        if (instantOfFlag) {
            return true;
        }

        return false;
    }

    /**
     * 判断对象是否属于文件类型
     */
    @SuppressWarnings("all")
    public static <T> boolean matchObj(Object obj, Class<T> clazz) {
        Class<?> objClass = obj.getClass();

        // 对象
        if (clazz.isAssignableFrom(objClass)) {
            return true;

        }

        return false;
    }

    @SuppressWarnings("all")
    public static <T> boolean matchCollection(Object obj, Class<T> clazz) {
        Class<?> objClass = obj.getClass();

        // 集合
        boolean instantOfFlag = Collection.class.isAssignableFrom(objClass)
                && CollectionUtils.isNotEmpty((Collection) obj)
                && clazz.isAssignableFrom(((Collection) obj).toArray()[0].getClass());
        if (instantOfFlag) {
            return true;
        }

        return false;
    }

    /**
     * 字符串为{@code null}则返回空串，否则返回自身
     *
     * @deprecated {@link StringUtils#stripToEmpty(String)}
     */
    @Deprecated
    public static String emptyStringIfNull(String val) {
        return val == null ? "" : val;
    }

    /**
     * 字符串为空串则返回{@code null}，否则返回自身
     *
     * @deprecated {@link StringUtils#stripToNull(String)}
     */
    @Deprecated
    public static String nullIfEmptyString(String val) {
        return StringUtils.isBlank(val) ? null : val;
    }
}
